<?php
$environment = 1;
?>
<div class="container" id="install-body">
    <div class="row">
        <div class="col-md-3">
            <div class="card border-primary mb-3">
                <div class="card-header">安装步骤</div>
                <div class="list-group list-group-flush">
                    <a href="#" class="list-group-item list-group-item-action ">
                        1.同意注册协议
                    </a>
                    <a href="#" class="list-group-item list-group-item-action ">
                        2.安装环境检测
                    </a>
                    <a href="#" class="list-group-item list-group-item-action ">
                        3.设置基础信息
                    </a>
                    <a href="#" class="list-group-item list-group-item-action active">
                        4.进行安装
                    </a>
                    <a href="#" class="list-group-item list-group-item-action ">
                        5.安装完成
                    </a>
                </div>
            </div>
        </div>
        <div class="col-md-9">
            <form id="form-database" action="index.php?r=install/default/install" method="post"  >
                <input type="hidden" name="YII_CSRF_TOKEN" value="<?php echo Yii::app()->request->csrfToken; ?>"/>
                <div class="card border-primary mb-3">
                    <div class="card-header">进行安装</div>
                    <div class="card-body">
                        <div class="progress mb-3">
                            <div id="install-progress" class="progress-bar progress-bar-striped progress-bar-animated" role="progressbar" aria-valuenow="75" aria-valuemin="0" aria-valuemax="100" style="width: 0%"></div>
                        </div>
                        <div class="form-group">
                            <textarea class="form-control" id="install-log" rows="20" style="resize: none;"></textarea>
                        </div>
                        <div class="form-group " id="alert-install" style="display: none;">
                            <div class="alert alert-dismissible alert-warning">
                                <button type="button" class="close" data-dismiss="alert">&times;</button>
                                <p class="mb-0 message">
                                </p>
                            </div>
                        </div>
                        <div class="form-group">
                            <button type="button" id="btn-finished" class="btn btn-primary btn-block btn-lg" disabled="disabled" onclick="finished()" >下一步</button>
                        </div>
                    </div>
                </div>
            </form>
        </div>
    </div>
</div>
<script>
    var installAll = true;
    $(function () {
        install();
    });
    function install() {
        /**
         * 1.执行SQL,设置管理员账户密码
         * 2.更新数据库配置文件
         * 3.更新fontend下的配置文件
         */
        installDB();
    }
    function installDB() {
        $.post('index.php?r=install/default/querysql',{
            YII_CSRF_TOKEN:"<?php echo Yii::app()->request->csrfToken; ?>"
        },function (data) {
            if(data.code != 10000){
                installAll = false;
                installFailure(data);
            }else{
                $("#install-progress").css("width","20%");
                updateDBConfig();
            }
        },'json');
    }
    function updateDBConfig() {
        $.post('index.php?r=install/default/updatedbconfig',{
            YII_CSRF_TOKEN:"<?php echo Yii::app()->request->csrfToken; ?>"
        },function (data) {
            if(data.code != 10000){
                installAll = false;
                installFailure(data);
            }else{
                $("#install-progress").css("width","40%");
                lockInstll();
            }
        },'json');
    }
    function lockInstll() {
        //设置install.lock文件，更新protected/config下的配置文件
        $.post('index.php?r=install/default/lockinstall',{
            YII_CSRF_TOKEN:"<?php echo Yii::app()->request->csrfToken; ?>"
        },function (data) {
            if(data.code != 10000){
                installAll = false;
                installFailure(data);
            }else{
                $("#install-progress").css("width","60%");
                removeInstallFiles();
            }
        },'json');
    }
    function removeInstallFiles() {
        //删除安装的配置文件
        $("#install-progress").css("width","100%");
        $("#btn-finished").removeAttr("disabled");
    }
    function installFailure(data) {
        if(installAll == false){
            $("#alert-install .message").text("安装出现异常，错误码："+data.code+"，请重试，如果仍然无法解决，请反馈至：http://www.ranko.cn");
            $("#alert-install").show();
        }
    }
    function finished() {
        window.location.href = "index.php?r=install/default/welcome";
    }
</script>